$(function () {

    var shopName=$("#shopName").text();
    var shopStatus=$("#shopStatus").text();
    var isBoolSeat=$("#isBoolSeat").text();
    var providerId=$("#providerId").text();
    var cityId=$("#cityId").text();

    //加载绑定子城市
    $.getJSON("/city/hasFatherId","fatherId="+providerId,function(data){
        $("#cityList").html("<option value='0'>请选择城市名称</option>");
        $.each(data,function(i,e){
            if(cityId==e.cityId){
                $("#cityList").append("<option value='"+e.cityId+"' selected>"+e.cityName+"</option>");
            }else{
                $("#cityList").append("<option value='"+e.cityId+"'>"+e.cityName+"</option>");
            }

        });
    });

    var fatherTypeId=$("#fatherTypeId").text();
    var childTypeId=$("#childTypeId").text();
   //绑定子类型
    $.getJSON("/showTypecontroller/father","fatherId="+fatherTypeId,function(data){
        $("#smallTypeList").html("<option value='0'>请选择子类名称</option>");
        $.each(data,function(i,e){
            if(childTypeId==e.showTypeId){
                $("#smallTypeList").append("<option value='"+e.showTypeId+"' selected>"+e.typeName+"</option>");
            }else{
                $("#smallTypeList").append("<option value='"+e.showTypeId+"'>"+e.typeName+"</option>");
            }

        });
    });

    var startTime=$("#startTime").text();
    var endTime=$("#endTime").text();
    var starLever=$("#starLever").text();

    /*alert(shopName+"   shopName "+shopStatus+"       shopStatus  "+isBoolSeat+"   isBoolSeat "+providerId+"       providerId  "+cityId+"   cityId");
    alert(fatherTypeId+"   fatherTypeId "+childTypeId+"       childTypeId  "+startTime+"   startTime "+endTime+"       endTime  "+starLever+"  starLever");*/
    page(shopName,shopStatus,isBoolSeat,providerId,cityId,fatherTypeId,childTypeId,startTime,endTime,starLever,$("#dataSizeCount").text(),$("#number").text(),2);


    //单选按钮选中事件
    $(":radio[name='isSeat'][value='" + isBoolSeat + "']").prop("checked", "checked");

    var id=$("#starLever").text();
    if (id==1){
        $("#xingji img:nth-of-type(1)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(2)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(3)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(4)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(5)").attr("src","/product_img/logo/star-off.png");
        $("#star").val(1);

    }
    if (id==2){
        $("#xingji img:nth-of-type(1)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(2)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(3)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(4)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(5)").attr("src","/product_img/logo/star-off.png");
        $("#star").val(2);
    }
    if (id==3){
        $("#xingji img:nth-of-type(1)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(2)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(3)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(4)").attr("src","/product_img/logo/star-off.png");
        $("#xingji img:nth-of-type(5)").attr("src","/product_img/logo/star-off.png");
        $("#star").val(3);
    }
    if (id==4){
        $("#xingji img:nth-of-type(1)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(2)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(3)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(4)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(5)").attr("src","/product_img/logo/star-off.png");
        $("#star").val(4);
    }
    if (id==5){
        $("#xingji img:nth-of-type(1)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(2)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(3)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(4)").attr("src","/product_img/logo/star-on.png");
        $("#xingji img:nth-of-type(5)").attr("src","/product_img/logo/star-on.png");
        $("#star").val(5);
    }

    /**
     * -------------cp
     * 删除商品点击事件
    */
    $(".del").click(function(){
        var id=this.id;
        var obj=this;
        $.ajax({
            type: "get",
            url: "/commodityController/isBoolDel",
            data:{"shopId":id},
            dataType: "json",
            success: function(data){

                if(data==true){
                    layer.confirm('确认要删除吗？',function(index){
                        $.getJSON("/commodityController/deteleCommodity","commodityId="+id,function (result) {
                            if (result=="1"){
                                alert("删除成功!!!")//删除成功后，刷新页面
                                location.href="/commodityController/info?pageNo="+$("#number").text()+
                                    "&size="+2+
                                    "&commodityName="+shopName+
                                    "&status="+shopStatus+
                                    "&isSeat="+isBoolSeat+
                                    "&provinceId="+providerId+
                                    "&cityId="+cityId+
                                    "&typeId="+fatherTypeId+
                                    "&clildTypeId="+childTypeId+
                                    "&startTime="+startTime+
                                    "&endTime="+endTime+
                                    "&star="+starLever;


                            }else{
                                layer.alert("网络连接失败!!!")
                            }
                        })
                    });
                }else if(data==false){
                    alert("对不起，你没有权限操作该商品");
                }
            },
            error: function(msg){
                alert( "网络连接异常！！！");
            }
        });


    });

   /* 商品上架*/
    $(".upShop").click(function(){
        /*获取要上架商品Id */
        var upShopId=this.id;
        $.ajax({
            type: "get",
            url: "/commodityController/isBoolDel",
            data:{"shopId":upShopId},
            dataType: "json",
            success: function(data){

                if(data==true){
                    layer.confirm('确认要上架该产品吗？',function(index){
                        $.getJSON("/commodityController/updateShopStatus","commodityId="+upShopId+"&status="+1,function(data){

                            /*  返回true,说明修改成功，然后刷新页面*/
                            if(data){
                                alert("商品已经成功上架！！！");

                                location.href="/commodityController/info?pageNo="+$("#number").text()+
                                    "&size="+2+
                                    "&commodityName="+shopName+
                                    "&status="+shopStatus+
                                    "&isSeat="+isBoolSeat+
                                    "&provinceId="+providerId+
                                    "&cityId="+cityId+
                                    "&typeId="+fatherTypeId+
                                    "&clildTypeId="+childTypeId+
                                    "&startTime="+startTime+
                                    "&endTime="+endTime+
                                    "&star="+starLever;
                            }
                        })

                    })
                }else if(data==false){
                    alert("对不起，你没有权限操作该商品");
                }
            },
            error: function(msg){
                alert( "网络连接异常！！！");
            }
        });

    });

    /*商品下架*/
   //在这个上面做修改，上面的是原份
    $(".downShop").click(function(){
        //获取要下架商品Id
        var downShopId=this.id;
        //根据商品Id获取商品信息
        $.ajax({
            type: "get",
            url: "/commodityController/isBoolDel",
            data:{"shopId":downShopId},
            dataType: "json",
            success: function(data){

               if(data==true){
                   layer.confirm('确认要下架该产品吗？',function(index){
                       $.getJSON("/commodityController/updateShopStatus","commodityId="+downShopId+"&status="+0,function(data){

                           /!*  返回true,说明修改成功，然后刷新页面*!/
                           if(data){
                               alert("商品已经成功下架！！！");

                               location.href="/commodityController/info?pageNo="+$("#number").text()+
                                   "&size="+2+
                                   "&commodityName="+shopName+
                                   "&status="+shopStatus+
                                   "&isSeat="+isBoolSeat+
                                   "&provinceId="+providerId+
                                   "&cityId="+cityId+
                                   "&typeId="+fatherTypeId+
                                   "&clildTypeId="+childTypeId+
                                   "&startTime="+startTime+
                                   "&endTime="+endTime+
                                   "&star="+starLever;
                           }else{
                               alert("商品下架失败！！！");
                           }
                       })

                   })
               }else if(data==false){
                   alert("对不起，你没有权限操作该商品");
               }
            },
            error: function(msg){
                alert( "网络连接异常！！！");
            }
        });


    });
})







/*分页，方法固定*/
function page(shopName,shopStatus,isBoolSeat,providerId,cityId,fatherTypeId,childTypeId,startTime,endTime,starLever,totaElements,pageNo,pageSize){

    layui.use('laypage', function(){
        var laypage = layui.laypage
        //执行一个laypage实例
        var pageNum=parseInt(pageNo)+1;

        laypage.render({
            elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
            ,count: totaElements //数据总数，从服务端得到
            ,curr:pageNum  //页码
            ,limit:pageSize  //每一页显示的条数
            ,skip:true
            ,jump: function(obj, first) {
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                var aaa=obj.curr-1;
                if(!first){
                    location.href="/commodityController/info?pageNo="+aaa+
                        "&size="+pageSize+
                        "&commodityName="+shopName+
                        "&status="+shopStatus+
                        "&isSeat="+isBoolSeat+
                        "&provinceId="+providerId+
                        "&cityId="+cityId+
                        "&typeId="+fatherTypeId+
                        "&clildTypeId="+childTypeId+
                        "&startTime="+startTime+
                        "&endTime="+endTime+
                        "&star="+starLever;

                }
            }
        });
    });
}






function select(page) {
    var commodityName=$("#commodityName").val();//商品名称
    //商品状态(0下架、1上架、2暂停售卖、3过期)
    var shopStatus=$('#statusList option:selected') .val();//选中的值   商品状态
    //var  isSeat=$('#isSeatList option:selected') .val();//选中的值   是否可以选座
    var isSeat=$("input[name='isSeat']:checked").val(); ///是否字词在线选座
    var  fatherType=$('#typeList option:selected') .val();//选中的值   大类型
    var  sonType=$('#smallTypeList option:selected') .val();//选中的值   小类型
    var  provinceId=$('#provinceList option:selected') .val();//选中的值   省会
    var  cityId=$('#cityList option:selected') .val();//选中的值   城市
    var  startTime=$("#start").val();
    var  endTime=$("#end").val();
    var starLever=sumstar;


   // $("#showTable").html("");
  alert(commodityName+"  commodityName");
    /*alert(shopStatus+"  shopStatus");
   alert(isSeat+"  isSeat");
   alert(fatherType+"  fatherType");
   alert(sonType+"  sonType");
   alert(provinceId+"  provinceId");
   alert(cityId+"  cityId");
   alert(startTime+"  startTime");
   alert(endTime+"  endTime");*/


   /* $.getJSON("/commodityController/testPage","commodityName="+commodityName+"&provinceId="+provinceId+"&cityId="+cityId+"&typeId="+typeList+"&clildTypeId="+smallTypeList+"&status="+status+"&star="+sumstar+"&isSeat="+isSeat+"&startTime="+startTime+"&endTime="+endTime+"&page="+page, function (result) {
        thisPage=result.number;
        sumPage=result.totalPages;
        $("#thisPage").text(thisPage+1);
        $("#sumPage").text(sumPage);
        $.each(result.content, function (i, e) {
            var status= e.status;//状态
            var status1="";
            if (status==0){
                status1="下架";
            }else if (status==1){
                status1="上架";
            }else if (status==2){
                status1="暂停售卖";
            }else if (status==3){
                status1="过期";
            }
            var isSeat= e.isSeat;//是否可选择座位,0不能，1能
            var Seat="";
            if (isSeat==1){
                Seat="是";
            }else {
                Seat="否";
            }

            $("#showTable").append("<tr id='"+e.commodityId+"'>\n" +
                "                            <td width=\"25px\"><label><input type=\"checkbox\" class=\"ace\" ><span class=\"lbl\"></span></label></td>\n" +
                "                            <td width=\"50px\">"+e.commodityId+"</td>\n" +
                "                            <td width=\"50px\">"+e.typeId+"</td>\n" +
                "                            <td width=\"120px\">"+e.cityId+"</td>\n" +
                "                            <td width=\"180px\">"+e.commodityName+"</td>\n" +
                "                            <td width=\"180px\">"+e.startTime+"</td>\n" +
                "                            <td width=\"100px\">"+e.star+"</td>\n" +
                "                            <td width=\"80px\">"+status1+"</td>\n" +
                "                            <td width=\"80px\">"+Seat+"</td>\n" +
                "                            <td width=\"80px\">"+e.buyAmount+"</td>\n" +
                "                            <td class=\"td-manage\">\n" +
                "                                <a title=\"编辑\" onclick=\"member_edit('编辑','member-add.html','4','','510')\" href=\"javascript:;\"  class=\"btn btn-xs btn-info\" >编辑</a>\n" +
                "                                <a title=\"删除\" href=\"javascript:;\"  onclick=\"member_del(this,'"+e.commodityId+"')\" class=\"btn btn-xs btn-delete\" >删除</a>\n" +
                "                            </td>\n" +
                "                        </tr>");

        });
    });*/
}

